package com.grape.api.v1;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.domain.Bill;
import com.grape.service.IBillService;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.dto.DailyDTO;
import com.grape.dto.StatisticalCurrentMonthDTO;
import com.grape.dto.StatisticalMonthChartDTO;
import com.grape.dto.StatisticalMonthDTO;
import com.grape.req.StatisticYearMonthReq;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.vo.DailyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.grape.annotation.RepeatSubmit;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 账单表 控制器
 * @author      putao
 * @email       2628279194@qq.com
 */
@RestController
@RequestMapping("bills")
@Api(tags = "账单接口")
@AllArgsConstructor
public class BillController extends BaseController<Bill> {

    private final IBillService billService;


    @GetMapping
    @ApiOperation(value = "账单分页")
    @OperateMethodName(value = "账单表列表分页", url = "account:bills:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<Bill> pageInfo = billService.pageList(baseVo);
        List<Bill> billList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(billList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @GetMapping("listPageCustom")
    @ApiOperation(value = "账单自定义分页")
    @OperateMethodName(value = "账单表列表", url = "account:bills:listPageCustom")
    public R<?> pageListCustom(BaseVo baseVo) {
        Page<Bill> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        billService.pageListCustom(baseVo);
        PageInfo<Bill> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @RepeatSubmit
    @PutMapping
    @ApiOperation(value = "账单修改")
    @OperateMethodName(value = "账单表修改", url = "account:bills:update")
    public R<?> update(@Validated @RequestBody Bill bill) {
        if (!Strings.isNotBlank(bill.getId())) {
            return updateNotFound(bill, null);
        }
        billService.updateData(bill);
        return updateSuccess(null, null);
    }


    @RepeatSubmit
    @PostMapping
    @ApiOperation(value = "添加账单")
    @OperateMethodName(value = "添加账单表", url = "account:bills:insert")
    public R<?> insert(@Validated @RequestBody Bill bill) {
        return insertSuccess(billService.insertData(bill), null);
    }


    @DeleteMapping
    @ApiOperation(value = "删除账单，可批量多个id逗号拼接")
    @OperateMethodName(value = "账单表删除,批量", url = "account:bills:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空") @RequestParam String ids) {
        int number = billService.deleteData(ids);
        if (number < 1) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }


    @GetMapping("detail")
    @ApiOperation(value = "账单详情")
    @OperateMethodName(value = "账单表详情", url = "account:bills:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        Bill bill = billService.detail(id);
        if (bill != null) {
            return selectSuccess(bill, id);
        }
        return selectNotFound(id, null);
    }

    @PostMapping("getDailyList")
    @ApiOperation(value = "按天查询账单")
    @OperateMethodName(value = "按天查询账单", url = "account:bills:dailyDTOList")
    public R<List<DailyDTO>> getDailyList(@Validated @RequestBody DailyVO dailyVO) {
        List<DailyDTO> dailyDTOList = billService.dailyDTOList(dailyVO);
        return selectSuccess(dailyDTOList, null);
    }


    @GetMapping("getCurrentMonthStatistical")
    @ApiOperation(value = "获取当前月份的统计数据 首页")
    @OperateMethodName(value = "获取当前月份的统计数据 首页", url = "system:users:getCurrentMonthStatistical")
    public R<StatisticalCurrentMonthDTO> getCurrentMonthStatistical(@NotBlank(message = "账本id不能为空") @RequestParam("accountBookId") String accountBookId) {
        return R.selectSuccess(billService.getCurrentMonthStatistical(accountBookId), null);
    }

    @GetMapping("statisticMonthChart")
    @ApiOperation(value = "获取月份的统计数据 统计页面")
    @OperateMethodName(value = "获取月份的统计数据 统计页面", url = "system:users:statisticMonthChart")
    public R<StatisticalMonthChartDTO> statisticMonthChart(@NotBlank(message = "账本id不能为空") @RequestParam("accountBookId") String accountBookId, @NotBlank(message = "类型不能为空") @RequestParam("type") String type) {
        return R.selectSuccess(billService.statisticMonthChart(accountBookId, type), null);
    }

    @PostMapping("statisticByYearMonth")
    @ApiOperation(value = "获取指定年月的统计数据 统计页面")
    @OperateMethodName(value = "获取指定年月的统计数据 统计页面", url = "system:users:statisticByYearMonth")
    public R<StatisticalMonthDTO> statisticByYearMonth(@Validated @RequestBody StatisticYearMonthReq statisticYearMonthReq) {
       return R.selectSuccess(billService.statisticByYearMonth(statisticYearMonthReq), null);
    }

}

